<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
        1 递归法：
          时间复杂度O(n)
          空间O(n)
        */
        var isSubtree = function (s, t) {
            if (s === null) {
                return false
            }
            // 根据题目范围来看，一定是有节点的
            if (isSameTree(s, t)) {
                return true
            }
            // 必须写isSubtree，因为isSubtree这个方法比较，如果左节点不符合条件，则还会去看他的左右子节点
            return isSubtree(s.left, t) || isSubtree(s.right, t)
        };
        function isSameTree(s, t) {
            if (s === null && t === null) {
                return true
            }
            if (s === null || t === null) {
                return false
            }
            // 如果s和t的val不相等，直接return false了。
            if (s.val !== t.val) {
                return false
            }
            return isSameTree(s.left, t.left) && isSameTree(s.right, t.right)
        }


        function Node(val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
        var a = new Node(1)
        var b = new Node(2)
        var c = new Node(3)
        var d = new Node(2)
        var e = new Node(1)
        var f = new Node(2)
        var g = new Node(3)
        // var g = new Node(3)
        a.left = b
        a.right = c
        d.left = e
        e.left = f
        e.right = g
        console.log(a);
        console.log(d);
        console.log(isSubtree(d, e));
    </script>
</body>

</html>